<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div id="registerdiv" class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.html">登录</a>
    </div>
    <form id="reg-form" action="" method="post">
        <table>
            <tr>
                <td>学号</td>
                <td class="inputs">
                    <input name="id" v-model="student.id" type="text" id="id">
                    <br>
                    <span id="id_err" class="err_msg" style="display: none">学号已存在</span>
                </td>
            </tr>
            <tr>
                <td>学生姓名</td>
                <td class="inputs">
                    <input name="name" v-model="student.name" type="text" id="name">
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" v-model="student.password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>
            <tr>
                <td>学院</td>
                <td>
                    <label for="department">
                        <select id="department" name="department" v-model="student.departmentName" @blur="queryMajors">
                            <option disabled value="">请选择学院</option>
                            <option v-for="department in departments" v-bind:value="department.name">
                                {{department.name}}
                            </option>
                        </select>
                    </label>
                </td>
            </tr>
            <tr>
                <td>专业</td>
                <td>
                    <label for="department">
                        <select id="major" name="major" v-model="student.majorName">
                            <option disabled value="">请选择专业班级</option>
                            <option v-for="major in majors" v-bind:value="major.name">
                                {{major.name}}
                            </option>
                        </select>
                    </label>
                </td>
            </tr>
        </table>
        <div class="buttons">
            <input value="注 册" type="button" @click="submitForm" id="reg_btn">
        </div>
        <br class="clear">
    </form>
</div>

<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>

<script>
    new Vue({
        el: "#registerdiv",
        data() {
            return {
                majors: {},
                departments: {},
                student: {}
            }
        },
        methods: {
            queryMajors() {
                var _this = this;
                let param = new URLSearchParams()
                param.append('q','major')
                param.append('departmentName',_this.student.departmentName)
                axios({
                    method: "post",
                    url: "/coursesystem/DepartmentMajorServlet",
                    data: param
                }).then(function (resp) {
                    console.log(resp.data)
                    _this.majors = resp.data.majors
                })
            },
            submitForm() {
                // 发送ajax请求，添加
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/coursesystem/registerServlet",
                    data: _this.student
                }).then(function (resp) {
                    alert(resp.data);
                    // 判断响应数据是否为 success
                    if (resp.data === "success") {
                        _this.student = {}
                        location.href = "http://localhost:8080/coursesystem/login.html";
                    }
                })

            }
        },
        mounted: function () {
            var _this = this;
            let param = new URLSearchParams()
            param.append('q','department')
            axios({
                method: "post",
                url: "/coursesystem/DepartmentMajorServlet",
                data: param
            }).then(function (resp) {
                console.log(resp.data)
                _this.departments = resp.data.departments
            })
        }
    })
</script>
</body>
</html>